package cn.hfatec.shms.web.xc.xcTdhgl.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.HashMap;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.web.xc.xcTdhgl.model.XcTdhgl;
import cn.hfatec.shms.web.xc.xcTdhgl.dao.XcTdhglDao;
import cn.hfatec.shms.web.xc.xcTdhgl.model.XcTdhglParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("XcTdhglService")
public class XcTdhglService {

    public static final String BEAN_ID = "XcTdhglService";

    //注入Dao层
    @Resource(name = "XcTdhglDao")
    private XcTdhglDao xcTdhglDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param xcTdhglParams
     * @param page
     *
     */
    @Transactional
    public Table queryXcTdhglTable(XcTdhglParams xcTdhglParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();


        String bzStart = null;
        if ((xcTdhglParams.getTjStart() == null) || (xcTdhglParams.getTjStart().isEmpty())) {
            bzStart = getBzstart();
            xcTdhglParams.setTjStart(bzStart);
        } else {
            bzStart = xcTdhglParams.getTjStart();
        }
        if ((xcTdhglParams.getTjEnd() == null) || (xcTdhglParams.getTjEnd().isEmpty())) {
            String riqiend = getBzend(bzStart);
            xcTdhglParams.setTjEnd(riqiend);
        }
        params.put("params", xcTdhglParams);
        return baseDao.queryTableByPage("queryXcTdhgl", params, page);
    }
    public String getBzstart() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.add(Calendar.DATE, -1);
        String bzstart = df.format(cal.getTime());
        return bzstart;
    }

    public String getBzend(String bzStart) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date myDate = null;
        String bzend = null;
        try {
            myDate = formatter.parse(bzStart);
            Calendar c = Calendar.getInstance();
            c.setTime(myDate);
            c.add(Calendar.DATE, 6);
            myDate = c.getTime();
            bzend = formatter.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bzend;
    }
    /**
     *
     * 插入数据
     * @param xcTdhgl
     *
     */
    @Transactional
    public void saveXcTdhgl(XcTdhgl xcTdhgl) {
        if(xcTdhgl.getId() != null) {
            XcTdhgl result = getXcTdhgl(xcTdhgl.getId());
            if(result != null) {
                xcTdhglDao.update(xcTdhgl);
            } else {
                xcTdhglDao.insert(xcTdhgl);
            }
        } else {
            xcTdhglDao.insert(xcTdhgl);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteXcTdhgl(Integer id) {

        xcTdhglDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param xcTdhglList
     *
     */
    @Transactional
    public void deleteXcTdhglBatch(List<String> xcTdhglList) {
        for(String id : xcTdhglList) {
            deleteXcTdhgl(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public XcTdhgl getXcTdhgl(Integer id) {

        return xcTdhglDao.getXcTdhglById(id);
    }
}